package cn.wolfcode.web.controller;

import cn.wolfcode.common.domain.UserInfo;
import cn.wolfcode.common.exception.BusinessException;
import cn.wolfcode.common.web.Result;
import cn.wolfcode.common.web.anno.RequireLogin;
import cn.wolfcode.domain.OrderInfo;
import cn.wolfcode.domain.SeckillProductVo;
import cn.wolfcode.mq.MQConstant;
import cn.wolfcode.mq.OrderMessage;
import cn.wolfcode.redis.SeckillRedisKey;
import cn.wolfcode.service.IOrderInfoService;
import cn.wolfcode.service.ISeckillProductService;
import cn.wolfcode.util.UserUtil;
import cn.wolfcode.web.msg.SeckillCodeMsg;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.awt.print.Pageable;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;


@RestController
@RequestMapping("/order")
@Slf4j
public class OrderInfoController {
    @Autowired
    private ISeckillProductService seckillProductService;
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private RocketMQTemplate rocketMQTemplate;
    @Autowired
    private IOrderInfoService orderInfoService;

    public static final ConcurrentHashMap<Long,Boolean> local_map=new ConcurrentHashMap();

    @RequestMapping("/doSeckill")
    @RequireLogin
    public Result<String> doSeckill(Integer time, Long seckillId, HttpServletRequest request){
        //1.判断是否登录,贴注解判断是否登录
        Boolean aBoolean = local_map.get(seckillId);
        if(aBoolean!=null&&aBoolean){
            throw new BusinessException(SeckillCodeMsg.SECKILL_STOCK_OVER);
        }
        //2.判断参数是否为空
        if(StringUtils.isEmpty(time+"") &&StringUtils.isEmpty(seckillId+"")){
            throw new BusinessException(SeckillCodeMsg.SECKILL_PARAM_ERROR);
        }
        //3.判断商品是否再秒杀活动时间内
        /**
         * 参数1:开始时间从vo获取开始时间
         * 场次
         */
        SeckillProductVo vo = seckillProductService.find(time, seckillId);
        /*boolean legalTime = DateUtil.isLegalTime(vo.getStartDate(), time);
        if(!legalTime){
            throw new BusinessException(SeckillCodeMsg.SECKILL_ERROR);
        }*/
        //4.判断库存是否足够
        Integer stockCount = vo.getStockCount();
        if (stockCount<0){
            ConcurrentMap map=new ConcurrentHashMap();
            map.put(seckillId,true);
            throw new BusinessException(SeckillCodeMsg.SECKILL_STOCK_OVER);
        }
        //5.判断是否重复下单
        String token = request.getHeader("token");
        UserInfo user = UserUtil.getUser(redisTemplate, token);
        Boolean member = redisTemplate.opsForSet().isMember(SeckillRedisKey.SECKILL_ORDER_SET.getRealKey(time + ""), user.getPhone() + ":" + seckillId);
        if (member){
            throw new BusinessException(SeckillCodeMsg.REPEAT_SECKILL);
        }
        Long increment = redisTemplate.opsForHash().increment(SeckillRedisKey.SECKILL_STOCK_COUNT_HASH.getRealKey(time + ""), seckillId+"", -1L);
        if(increment<0){
            local_map.put(seckillId,true);
            throw new BusinessException(SeckillCodeMsg.SECKILL_STOCK_OVER);
        }
        //把数据封装成对象发送到mq中
        OrderMessage orderMessage=new OrderMessage();
        orderMessage.setSeckillId(seckillId);
        orderMessage.setTime(time);
        orderMessage.setToken(token);
        orderMessage.setUserPhone(user.getPhone());
        rocketMQTemplate.syncSend(MQConstant.ORDER_PEDDING_TOPIC,orderMessage);

        //6.秒杀逻辑
        return Result.success("正在抢购中....");
    }
    @RequestMapping("find")
    public Result<OrderInfo> find(String orderNo){
       OrderInfo orderInfo= orderInfoService.find(orderNo);
       return  Result.success(orderInfo);
    }
}
